package com.meida.module.bus.provider.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.bus.client.entity.BusProject;
import com.meida.module.bus.client.entity.BusReportOutfive;
import com.meida.module.bus.provider.mapper.BusReportOutfiveMapper;
import com.meida.module.bus.provider.service.BusReportOutfiveService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 对外验工月报5接口实现类
 *
 * @author flyme
 * @date 2020-11-23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusReportOutfiveServiceImpl extends BaseServiceImpl<BusReportOutfiveMapper, BusReportOutfive> implements BusReportOutfiveService {


    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusReportOutfive bro, EntityMap extra) {
        return ResultBody.ok();
    }



    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusReportOutfive> cq, BusReportOutfive bro, EntityMap requestMap) {
        cq.eq("bro.projectCode", requestMap.get("projectCode"));
        cq.like("bro.reportMonth",requestMap.get("reportMonth"));
        cq.like("bro.batchNumber",requestMap.get("batchNumber"));
        cq.orderByDesc("bro.reportMonth");
        cq.orderByDesc("bro.batchNumber");
        cq.createJoin(BusProject.class).setMainField("projectCode").setJoinField("projectCode");
        return ResultBody.ok();
    }

    @Override
    public ResultBody importBusReportOutfivedata(MultipartFile file, Map params) {
        try{
            String reportMonth = (String) params.get("reportMonth");
            Integer batchNumber = TypeUtils.castToInt(params.get("batchNumber"));
            ExcelReader excelReader =ExcelUtil.getReader(file.getInputStream(), 0);
            List<List<Object>> read = excelReader.read(3);
            List<BusReportOutfive> outfives=new ArrayList<>();
            for(List<Object> objectObjectMap : read) {

                BusReportOutfive outfive=new BusReportOutfive();
                outfive.setReportMonth(reportMonth);
                outfive.setBatchNumber(batchNumber);
                outfive.setReportOutfiveId(IdWorker.getId());
                outfive.setProjectCode(params.get("projectCode").toString());
                outfive.setField1(objectObjectMap.get(0).toString());
                outfive.setRepotItem(objectObjectMap.get(1).toString());
                outfive.setProjectDesc(objectObjectMap.get(2).toString());
                outfive.setItemunit(objectObjectMap.get(3).toString());
                outfive.setQuantity(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(4).toString())));
                outfive.setSupplyPrice(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(5).toString())));
                outfive.setInstallPrice(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(5).toString())));
                outfive.setSynthesizeAmount(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(7).toString())));
                outfive.setRemark(objectObjectMap.get(8).toString());
                outfive.setAccumulativeCount(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(9).toString())));
                if(FlymeUtils.isNotEmpty(objectObjectMap.get(10))){
                    outfive.setAccumulativeAmount(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(10).toString())));
                }
                if(FlymeUtils.isNotEmpty(objectObjectMap.get(11))){
                    outfive.setReviewedCount(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(11).toString())));
                }
                if(FlymeUtils.isNotEmpty(objectObjectMap.get(12))){
                    outfive.setReviewedAmount(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(12).toString())));
                }
                if(FlymeUtils.isNotEmpty(objectObjectMap.get(13))){
                    outfive.setReviewedCent(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(13).toString())));
                }
                if(FlymeUtils.isNotEmpty(objectObjectMap.get(14))){
                    outfive.setRefCount(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(14).toString())));
                }
                if(FlymeUtils.isNotEmpty(objectObjectMap.get(15))){
                    outfive.setRefAmount(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(15).toString())));
                }
                if(FlymeUtils.isNotEmpty(objectObjectMap.get(16))){
                    outfive.setRefCent(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(16).toString())));
                }
                if(FlymeUtils.isNotEmpty(objectObjectMap.get(17))){
                    outfive.setApprovalCount(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(17).toString())));
                }
                if(FlymeUtils.isNotEmpty(objectObjectMap.get(18))){
                    outfive.setApprovalTotalPrice(BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get(18).toString())));
                }
                outfives.add(outfive);
            }
            boolean a = saveBatch(outfives);
        }catch (Exception e){
            e.printStackTrace();
        }
      return ResultBody.ok();
    }

    @Override
    public List<EntityMap> getExportData(Map params) {
        CriteriaQuery<BusReportOutfive> cq =new CriteriaQuery<BusReportOutfive>(BusReportOutfive.class);
        cq.eq("projectCode", params.get("projectCode"));
        cq.eq("reportMonth", params.get("reportMonth"));
        cq.eq("batchNumber", params.get("batchNumber"));
        return selectEntityMap(cq);
    }
}
